package org.jeecg.modules.workflow.controller.definition;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.workflow.vo.definition.model.*;
import org.jeecg.modules.workflow.controller.BaseController;
import org.jeecg.modules.workflow.convert.definition.BpmModelConvert;
import org.jeecg.modules.workflow.service.definition.BpmModelService;
import org.jeecg.modules.workflow.utils.IoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;


@Api(tags = "管理后台 - 流程模型")
@RestController
@RequestMapping("/bpm/model")
@Validated
public class BpmModelController extends BaseController {

    @Resource
    private BpmModelService bpmModelService;

    @Autowired
    private BpmModelConvert bpmModelConvert;

    @GetMapping("/page")
    @ApiOperation(value = "获得模型分页")
    public Result<IPage<BpmModelPageItemRespVO>> getModelPage(BpmModelPageReqVO pageVO) {
        IPage<BpmModelPageItemRespVO> pageResult = bpmModelService.getModelPage(pageVO);
        return Result.OK(pageResult);
    }

    @GetMapping("/get")
    @ApiOperation("获得模型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    //@RequiresPermissions("bpm:model:query")
    public Result<BpmModelRespVO> getModel(@RequestParam("id") String id) {
        BpmModelRespVO model = bpmModelService.getModel(id);
        return Result.OK(model);
    }

    @PostMapping("/create")
    @ApiOperation(value = "新建模型")
    //@RequiresPermissions("bpm:model:create")
    public Result<String> createModel(@Valid @RequestBody BpmModelCreateReqVO createRetVO) {
        return Result.OK("",bpmModelService.createModel(createRetVO, null));
    }

    @PostMapping("/import")
    @ApiOperation(value = "导入模型")
    //@RequiresPermissions("bpm:model:import")
    public Result<String> importModel(@Valid BpmModeImportReqVO importReqVO) throws IOException {
        BpmModelCreateReqVO createReqVO = bpmModelConvert.convert(importReqVO);
        // 读取文件
        String bpmnXml = IoUtils.readUtf8(importReqVO.getBpmnFile().getInputStream(), false);
        return Result.OK("",bpmModelService.createModel(createReqVO, bpmnXml));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改模型")
    //@RequiresPermissions("bpm:model:update")
    public Result<Boolean> updateModel(@Valid @RequestBody BpmModelUpdateReqVO modelVO) {
        bpmModelService.updateModel(modelVO);
        return Result.OK(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除模型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    //@RequiresPermissions("bpm:model:delete")
    public Result<Boolean> deleteModel(@RequestParam("id") String id) {
        bpmModelService.deleteModel(id);
        return Result.OK(true);
    }

    @PostMapping("/deploy")
    @ApiOperation(value = "部署模型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    //@RequiresPermissions("bpm:model:deploy")
    public Result<Boolean> deployModel(@RequestParam("id") String id) {
        bpmModelService.deployModel(id);
        return Result.OK(true);
    }

    @PutMapping("/update-state")
    @ApiOperation(value = "修改模型的状态", notes = "实际更新的部署的流程定义的状态")
    //@RequiresPermissions("bpm:model:update")
    public Result<Boolean> updateModelState(@Valid @RequestBody BpmModelUpdateStateReqVO reqVO) {
        bpmModelService.updateModelState(reqVO.getId(), reqVO.getState());
        return Result.OK(true);
    }

}
